package com.zzyl.serve.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.serve.domain.Reservation;
import com.zzyl.serve.dto.ReservationDto;
import com.zzyl.serve.service.IReservationService;
import com.zzyl.serve.vo.PageVO;
import com.zzyl.serve.vo.TimeCountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
//        System.out.println("------" + userId);
        if (userId != null) {
            int count = reservationService.getTodayCancelledCount(userId);
            return R.ok(count);
        }
        return R.ok(0);
    }

    /**
     * 查询每个时间段剩余预约次数
     * @param timestamp
     * @return
     */
    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public AjaxResult getReservationCountByTime(@RequestParam("time") long timestamp) {
        List<TimeCountVO> data = reservationService.getReservationCountByTime(timestamp);
        return AjaxResult.success(data);
    }

    //新增预约
    @PostMapping()
    @ApiOperation("新增预约")
    public AjaxResult addReservation(@RequestBody Reservation reservation) {
        //新增预约
        //@RequestBody ReservationDto reservationDto
//        reservationService.addReservation(reservationDto);
        reservationService.insertReservation(reservation);
        return success();
    }

    //分页查询预约
    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public AjaxResult getByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status
    ){
        startPage();
        List<Reservation> reservationList = reservationService.getByPage(pageNum, pageSize, status);
        long total=reservationList.size();
        PageVO pageVO = new PageVO(total, reservationList);

        return AjaxResult.success(pageVO);
    }

    //取消预约，将status改为2
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancelReservation(@PathVariable Long id) {
        //只修改status，upadteTime,不修改其他
        Reservation reservation = reservationService.selectReservationById(id);
        reservation.setStatus(2);
        reservationService.updateReservation(reservation);
        return success();
    }
}